﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using learncoremvc.EF;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;

namespace learncoremvc
{
    //https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/new-db
    public class Startup
    {
     public  static  int i = 0;
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            //var connection = @"Server=127.0.0.1;Database=CoreLearn;Trusted_Connection=True;ConnectRetryCount=0;user id=sa;password=123456;";
            //services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));
            var connection2 = @"Server=10.1.0.28\SQLEXPRESS;Database=CoreLearncontext;Trusted_Connection=True;ConnectRetryCount=0;user id=sa;password=123;";
            services.AddDbContext<LightContext>(options => options.UseSqlServer(connection2));
            services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection2));
            //services.AddTransient<LightContext>(factory =>
            //{
            //    //var builder = new DbContextOptionsBuilder<LightContext>();
            //    //builder.UseSqlServer(connection);

            //    var accessor = factory.GetService<IHttpContextAccessor>();
            //    bool? isDeleted = false;//默认全局查询未删除的数据

            //    if (accessor.HttpContext != null)
            //    {
            //        string method = accessor.HttpContext.Request.Method.ToLower();
            //        StringValues queryIsdeleted = StringValues.Empty;
            //        if (method == "get")
            //        {
            //            queryIsdeleted = accessor.HttpContext.Request.Query["d"];
            //        }
            //        else if (method == "post" && accessor.HttpContext.Request.HasFormContentType)
            //        {
            //            queryIsdeleted = accessor.HttpContext.Request.Form["d"];
            //        }
            //        if (!StringValues.IsNullOrEmpty(queryIsdeleted))
            //        {
            //            int isDeletedInt = 0;//0：未删除，1：已删除，2：全部
            //            if (int.TryParse(queryIsdeleted.FirstOrDefault(), out isDeletedInt))
            //            {
            //                if (isDeletedInt == 0)
            //                {
            //                    isDeleted = false;
            //                }
            //                else if (isDeletedInt == 1)
            //                {
            //                    isDeleted = true;
            //                }
            //                else if (isDeletedInt == 2)
            //                {
            //                    isDeleted = null;
            //                }
            //            }
            //        }
            //    }
            //    return new LightContext(builder.Options, isDeleted);
            //});
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
